package demo.test;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import demo.db.factory.HibernateUtil;

public class EventManagerServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		try {
			HibernateUtil.getSessionFactory().getCurrentSession().beginTransaction();
			// Process request and render page...
			// Write HTML header
			PrintWriter out = response.getWriter();
			out.println("<html><head><title>Event Manager</title></head><body>");

			// Handle actions
			if ("store".equals(request.getParameter("action"))) {

				String eventTitle = request.getParameter("eventTitle");
				String eventDate = request.getParameter("eventDate");

				if ("".equals(eventTitle) || "".equals(eventDate)) {
					out.println("<b><i>Please enter event title and date.</i></b>");
				} else {
					out.println("<b><i>Added event.</i></b>");
				}
			}

			// Write HTML footer
			out.println("</body></html>");
			out.flush();
			out.close();

			HibernateUtil.closeSession();
		} catch (Exception ex) {
			HibernateUtil.getSessionFactory().getCurrentSession().getTransaction().rollback();
			throw new ServletException(ex);
		}
	}

}
